package com.nclg.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nclg.exam.dto.CreaterDto;
import com.nclg.exam.dto.ExamQuestionDto;
import com.nclg.exam.dto.QuestionDto;
import com.nclg.exam.dto.QuestionListDto;
import com.nclg.exam.entity.TQuestion;
import com.nclg.exam.mapper.QuestionMapper;
import com.nclg.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询出题人
     * @return
     */
    @Override
    public List<CreaterDto> selectCreaters() {
        return questionMapper.selectCreaters();
    }

    /**
     * 查询题库
     * @param dto
     * @return
     */
    @Override
    public PageInfo<QuestionListDto> selectByPage(QuestionDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<QuestionListDto> list = questionMapper.selectByPage(dto);
        return new PageInfo<>(list);
    }

    /**
     * 创建题目
     * @param question
     * @return
     */
    @Override
    public boolean insert(TQuestion question) {
        return questionMapper.insert(question) > 0 ? true : false;
    }

    /**
     * 修改题目状态
     * @param qId
     * @param qstatus
     * @return
     */
    @Override
    public boolean updateStatus(Long qId, String qstatus) {
        return questionMapper.updateStatus(qId, qstatus) > 0 ? true : false;
    }

    /**
     * 查询题目
     * @param qId
     * @return
     */
    @Override
    public TQuestion selectById(Long qId) {
        return questionMapper.selectById(qId);
    }

    /**
     * 编辑题目
     * @param question
     * @return
     */
    @Override
    public boolean update(TQuestion question) {
        return questionMapper.update(question) > 0 ? true : false;
    }

    /**
     * 根据pid查询所有题目
     * @param pid
     * @return
     */
    @Override
    public List<ExamQuestionDto> selectQuestionByPid(Long pid) {
        return questionMapper.selectQuestionByPid(pid);
    }


}
